﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public class Torch : MapObject
    {
        //constructor
        public Torch()
            : base()
        {
            CanTake = true;
            CanReady = true;
            HandsRequired = 1;
            Flamability = 5;
            LootLevel = 0;
            Lifetime = Globals.RandomIntFromTo(60, 120);
            Damage = new DamageExpressionList("1z3=b;1z3=f");
        }

        //clone
        public override MapObject Clone()
        {
            return new Torch();
        }

        //is it burning?
        public bool IsBurning
        {
            get
            {
                return _isBurning;
            }
            set
            {
                if (value != _isBurning)
                {
                    _isBurning = value;
                    if (IsBurning)
                        AddTag("FireSource");
                    else
                        RemoveTag("FireSource");
                }
            }
        }

        //burn when readied
        public override void Readied()
        {
            IsBurning = true;
            if (Owner.Level != null)
                Owner.Level.Host.DisplayMessage(Owner.DisplayName + " lights a torch.");
        }
        public override void UnReadied()
        {
            IsBurning = false;
        }

        //return illum map if burning
        public override ByteMatrix IllumMap
        {
            get
            {
                if (!IsBurning)
                    return null;
                else
                {
                    if (_illumMap == null)
                        _illumMap = new ByteMatrix(Properties.Resources.TorchMap);
                    return _illumMap;
                }
            }
        }

        //processing
        public override void ProcessImpulse()
        {
            if (IsBurning)
            {
                Lifetime--;
                if (Lifetime <= 0)
                {
                    if (Owner != null)
                        if (Owner.IsPC)
                            Owner.Level.Host.DisplayMessage(Owner.DisplayName + "'s torch burns out!");
                    Lifetime = Globals.RandomIntFromTo(60, 120);
                    IsBurning = false;
                    if (Owner != null)
                        Owner.UnreadyGear(this);
                    ConsumeOne();
                    if (Owner != null)
                        Owner.Level.Host.ActorUpdated(Owner);
                }
            }
        }

        //might break when used as a weapon
        public override bool BreaksWhenStressed(StressType st)
        {
            if (st == StressType.UsedAsWeapon)
                return Globals.RandomIntFromTo(1, 100) < 10;
            else
                return false;
        }

        //private members
        private ByteMatrix _illumMap = null;
        private bool _isBurning = false;
    }
}
